/** /////////////////////////////////////////////////////////////////////////////////
 * @author		Jaeger, Mathias
 * @function 	Dient zur Initialisierung des Leap Motion Controllers
 * @param		d
 /////////////////////////////////////////////////////////////////////////////////*/

function leapController() {
	// Aufruf aus Leap.js zur Controllererzeugung
	laneAssist.leapController = new Leap.Controller({
		frameEventName : "animationFrame"
	});

	console.log(laneAssist.leapController);

	deviceListenerSetup();
	
		laneAssist.leapController.connect();
};

function deviceListenerSetup() {

	// Verbindung zum Framework ueberpruefen
	laneAssist.leapController.on('connect', function() {
		console.log('Schnittstelle verfügbar');

	});

	// Trennen des Leap Controllers abfangen
	laneAssist.leapController.on('deviceConnected', function() {
		console.log('Herzlichen Glückwunsch, ihr Leap Motion Controller wurde erkannt!');
	});

	// Anbinden des Leap Controllers abfangen
	laneAssist.leapController.on('deviceDisconnected', function() {
		confirm('Leider wurde die Verbindung zu ihrem LeapMotion Controller getrennt.');
	});

	// Einsatzbereitschaft des Leap Controllers abfangen
	laneAssist.leapController.on('ready', function() {
		console.log("Ihr Leap Motion Controller ist startklar!");
	});

	// Kontrollschleife zur Datenerfassung aus dem Leap Controller
	laneAssist.leapController.on('frame', function(frameDaten) {

		// console.log(frameDaten);

		// First we loop through all of the hands that the frame sees
		for (var i = 0; i < frameDaten.hands.length; i++) {
			// For each hand we define it
			var hand = frameDaten.hands[i];

			// and get its position, so that it can be passed through
			// for drawing the connections
			laneAssist.leapMotionSteuerungPosition = leapPosition(frameDaten, hand.palmPosition);
		};
	});
}

function leapPosition(frameDaten, handPosition) {
	// Gets the interaction box of the current frame
	var iBox = frameDaten.interactionBox;

	// Gets the left border and top border of the box
	// In order to convert the position to the proper
	// location for the canvas
	var left = iBox.center[0] - iBox.size[0] / 2;

	// Takes our leap coordinates, and changes them so
	// that the origin is in the top left corner
	var x = handPosition[0] - left;

	// Divides the position by the size of the box
	// so that x and y values will range from 0 to 1
	// as they lay within the interaction box
	x /= iBox.size[0];

	// Uses the height and width of the canvas to scale
	// the x and y coordinates in a way that they
	// take up the entire canvas
	// x *= laneAssist.fensterBreite;

	// Returns the values, making sure to negate the sign
	// of the y coordinate, because the y basis in canvas
	// points down instead of up
	return [x];
};
